﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KSOAPlus
{
    public partial class SelectDw : Form
    {
        public SelectDw()
        {
            InitializeComponent();
        }

        Common Init = new Common();
        MsSqlHelper mssql = new MsSqlHelper();

        public string querystr = "";

        private void SelectDw_Load(object sender, EventArgs e)
        {
            query();
        }
        DataTable dt = new DataTable();

        public void query()
        {
            string sql = "select top 120 rtrim(dwbh) as dwbh,rtrim(danwbh) as danwbh,rtrim(dwmch) as dwmch,rtrim(lxr) as lxr,rtrim(ywy) as ywy,rtrim(zjm) as zjm from [mchk](nolock) where beactive='是' and (danwbh like '%" + querystr + "%' or dwmch like '%" + querystr + "%' or zjm like '%" + querystr + "%') order by dwmch";
            dt = mssql.ExecuteDt(sql);
            dGV.DataSource = EnToZhTitle(dt);
            Init.AutoSizeColumn(dGV);
            dGV.Columns["单位内码"].Visible=false;
            dGV.Columns["助记码"].Visible = false;
        }

        public DataTable EnToZhTitle(DataTable dt)
        {
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string ColumnName = mssql.GetString("select top 1 rtrim(chnname) chnname  from [fldlist](nolock) where fdname=rtrim('" + dt.Columns[i].ToString() + "')");
                if (!dt.Columns.Contains(ColumnName))
                {
                    dt.Columns[i].ColumnName = ColumnName;
                }
            }
            return dt;
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            querystr = txtSearch.Text;
            DataView dataView = new DataView();
            dataView = dt.DefaultView;
            dataView.RowFilter = "单位名称 like '*" + querystr + "*' or 单位名称 like '*" + querystr + "*'";
            DataTable dataSource = dataView.ToTable();
            dGV.DataSource = dataSource;
        }

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            int WM_KEYDOWN = 256;
            int WM_SYSKEYDOWN = 260;
            if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
            {
                switch (keyData)
                {
                    case Keys.Escape:
                        this.Close();//esc关闭窗体
                        break;
                }
            }
            return false;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                int col = dGV.CurrentCell.ColumnIndex;
                int row = dGV.CurrentCell.RowIndex;
                if (row != dGV.NewRowIndex)
                {
                    if (col == (dGV.Columns.Count - 1))
                    {
                        col = -1;
                        row++;
                    }
                    dGV.CurrentCell = dGV[col + 1, row];
                }
                e.Handled = true;
            }
            base.OnKeyDown(e);
        }

        private void dGV_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter || e.KeyData == Keys.Return)
            {
                int row = this.dGV.CurrentCell.RowIndex;//选定当前选定的单元格所处的行号
                int col = this.dGV.CurrentCell.ColumnIndex;//选定当前选定的单元格所处的列号
                string id = dGV.Rows[row].Cells[0].Value.ToString();//将单元格的内容显示到文本
                GetString(id);
                this.Close();
                this.Dispose();
            }
        }

        private void dGV_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int row = this.dGV.CurrentCell.RowIndex;//选定当前选定的单元格所处的行号
            int col = this.dGV.CurrentCell.ColumnIndex;//选定当前选定的单元格所处的列号
            string id = dGV.Rows[row].Cells[0].Value.ToString();//将单元格的内容显示到文本
            GetString(id);
            this.Close();
            this.Dispose();
        }

        public void GetString(string id)
        {
            DataTable dt=mssql.ExecuteDt("select rtrim(dwbh) dwbh,rtrim(danwbh) as danwbh,rtrim(dwmch) as dwmch from [mchk](nolock) where dwbh='"+id+"'");
            Instance.Initialize();
            Instance.instance.dwbh = dt.Rows[0]["dwbh"].ToString();
            Instance.instance.danwbh = dt.Rows[0]["danwbh"].ToString();
            Instance.instance.dwmch = dt.Rows[0]["dwmch"].ToString();
        }
    }
}